import 'package:flutter/material.dart';
import 'package:frontend/models/global.dart';
import 'package:frontend/models/groupMessages.dart';
import 'package:frontend/models/personalGroups.dart';
import 'package:frontend/models/personalMessages.dart';
import 'package:frontend/pages/groupChattingPage.dart';
import 'package:frontend/pages/personalChattingPage.dart';
import 'package:frontend/reqs/requests.dart';
import 'package:frontend/models/contacts.dart';

class ChattingIntroducePage extends StatefulWidget {
  @override
  State<ChattingIntroducePage> createState() => _ChattingIntroducePageState();
}

class _ChattingIntroducePageState extends State<ChattingIntroducePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('联系人'),
        ),
        body: Column(
          children: [
            /*FutureBuilder<PersonalGroups?>(
              future: fetchPersonalGroups(Global.profile.user!.id),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  if (snapshot.hasError) {
                    return const Icon(Icons.error);
                  } else {
                    if (snapshot.data == null) {
                      return Center(
                        child: Column(
                          children: [Text('无聊天群组')],
                        ),
                      );
                    } else {
                      return Scrollbar(
                        child: ListView(
                          shrinkWrap: true,
                          padding: const EdgeInsets.symmetric(vertical: 8),
                          children: [
                            for (int index = 0;
                                index < snapshot.data!.personalGroups.length;
                                index++)
                              TextButton(
                                onPressed: () {
                                  Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: ((context) =>
                                              groupChattingPage(snapshot
                                                  .data!
                                                  .personalGroups[index]
                                                  .group_id))));
                                },
                                child: ListTile(
                                  leading: ExcludeSemantics(
                                    child: CircleAvatar(
                                        child: Text(snapshot
                                            .data!
                                            .personalGroups[index]
                                            .group_name[0])),
                                  ),
                                  title: Text(snapshot
                                      .data!.personalGroups[index].group_name),
                                  subtitle: Text(snapshot
                                              .data!
                                              .personalGroups[index]
                                              .last_message_state ==
                                          "success"
                                      ? snapshot.data!.personalGroups[index]
                                          .last_message
                                      : ""),
                                ),
                              ),
                          ],
                        ),
                      );
                    }
                  }
                } else {
                  // 请求未结束，显示loading
                  return CircularProgressIndicator();
                }
              },
            ),*/
            FutureBuilder<contacts?>(
              future: fetchPersonalMessages_1(Global.profile.user!.id),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  if (snapshot.hasError) {
                    return const Icon(Icons.error);
                  } else {
                    if (snapshot.data == null) {
                      return Center(
                        child: Column(
                          children: [Text('无联系人')],
                        ),
                      );
                    } else {
                      return Scrollbar(
                        child: ListView(
                          shrinkWrap: true,
                          padding: const EdgeInsets.symmetric(vertical: 8),
                          children: [
                            for (int index = 0;
                                index < snapshot.data!.messages.length;
                                index++)
                              TextButton(
                                onPressed: () {
                                  Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: ((context) =>
                                              personalChattingPage(snapshot
                                                  .data!
                                                  .messages[index]
                                                  .another_user_id))));
                                },
                                child: ListTile(
                                  leading: ExcludeSemantics(
                                    child: CircleAvatar(
                                        child: Text(snapshot
                                            .data!
                                            .messages[index]
                                            .another_user_name[0])),
                                  ),
                                  title: Text(snapshot.data!.messages[index]
                                      .another_user_name),
                                  subtitle: Text(snapshot
                                      .data!.messages[index].last_message),
                                ),
                              ),
                          ],
                        ),
                      );
                    }
                  }
                } else {
                  // 请求未结束，显示loading
                  return CircularProgressIndicator();
                }
              },
            ),
          ],
        ));
  }
}
